package com.hfxt.dao.impl;

import com.hfxt.dao.UsesDao;
import com.hfxt.entity.Uses;
import com.hfxt.tool.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UsesDaoImpl implements UsesDao {

    //mybatis实现持久化操作的步骤
    public int getcount() {
        int count=0;
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=null;
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession sqlSession=null;
        // 1.加载核心配置文件
        try{
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2\sqlsessionfactroybuilder
            sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();

            //3\sqlsessionfactory
            sqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);

            //4\sqlsession   用sqlsession实现持久化操作的增删改查的
            sqlSession=sqlSessionFactory.openSession();

            //5\持久化   dao接口全限定名+id名
            //count=sqlSession.selectOne("com.hfxt.dao.UsesDao.getcount");
            count=sqlSession.getMapper(UsesDao.class).getcount();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return count;
    }

    @Override
    public List<Uses> getusesbypage(Integer pageIndex, Integer pageSize) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesbypage(pageIndex,pageSize);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public int getcount2(Uses uses) {
        SqlSession sqlSession= MyBatisUtil.getSession();
       int count=0;
        try{
            count=sqlSession.getMapper(UsesDao.class).getcount2(uses);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return count;
    }

    @Override
    public List<Uses> getusesbypage2(Integer pageIndex, Integer pageSize, Uses uses) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesbypage2(pageIndex,pageSize,uses);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }


    @Override
    //一级缓存 sqlSession
    //sqlSession关闭 刷新有直接关系

    //二级缓存  sqlSessionFactory
    //mybatis内置的二级缓存
    //借用外部的缓存插件  echeCache  osCache


    public Uses getUserById(Integer userid) {
        SqlSession sqlSession=MyBatisUtil.getSession();
        Uses user1=sqlSession.getMapper(UsesDao.class).getUserById(1);
        System.out.println(user1.getUsername());
        Uses user2=sqlSession.getMapper(UsesDao.class).getUserById(1);
        System.out.println(user2.getUsername());
        System.out.println(user1==user2);

        MyBatisUtil.closeSession();
        sqlSession=MyBatisUtil.getSession();
        Uses user3=sqlSession.getMapper(UsesDao.class).getUserById(1);
        Uses user4=sqlSession.getMapper(UsesDao.class).getUserById(1);
        System.out.println(user3==user4);


        return user1;
    }

    public List<Uses> getuses() {
        List<Uses> list=new ArrayList<Uses>();
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=null;
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession sqlSession=null;
        // 1.加载核心配置文件
        try{
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2\sqlsessionfactroybuilder
            sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();

            //3\sqlsessionfactory
            sqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);

            //4\sqlsession   用sqlsession实现持久化操作的增删改查的
            sqlSession=sqlSessionFactory.openSession();

            //5\持久化   dao接口全限定名+id名
            //list=sqlSession.selectList("com.hfxt.dao.UsesDao.getall");
            list=sqlSession.getMapper(UsesDao.class).getuses();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public List<Uses> getusesByUsername(String username) {
        List<Uses> list=new ArrayList<Uses>();
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=null;
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession sqlSession=null;
        // 1.加载核心配置文件
        try{
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2\sqlsessionfactroybuilder
            sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();

            //3\sqlsessionfactory
            sqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);

            //4\sqlsession   用sqlsession实现持久化操作的增删改查的
            sqlSession=sqlSessionFactory.openSession();

            //5\持久化   dao接口全限定名+id名
            //list=sqlSession.selectList("com.hfxt.dao.UsesDao.getall");
            list=sqlSession.getMapper(UsesDao.class).getusesByUsername(username);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public List<Uses> getusesByChoice(String choice) {
        List<Uses> list=new ArrayList<Uses>();
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=null;
        SqlSessionFactory sqlSessionFactory=null;
        SqlSession sqlSession=null;
        // 1.加载核心配置文件
        try{
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2\sqlsessionfactroybuilder
            sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();

            //3\sqlsessionfactory
            sqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);

            //4\sqlsession   用sqlsession实现持久化操作的增删改查的
            sqlSession=sqlSessionFactory.openSession();

            //5\持久化   dao接口全限定名+id名
            //list=sqlSession.selectList("com.hfxt.dao.UsesDao.getall");
            list=sqlSession.getMapper(UsesDao.class).getusesByChoice(choice);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public List<Uses> getusesByCondition1(String name, Integer money) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition1(name,money);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    public List<Uses> getusesByCondition2(Map<String,Object> maps) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition2(maps);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getusesByCondition3(Uses uses) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition3(uses);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //MyBatisUtil.closeSession();
        }
        return list;
    }

    public List<Uses> getusesByCondition4(Uses uses) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition4(uses);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public List<Uses> getusesByCondition5(Integer[] roleids) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition5(roleids);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }


    public List<Uses> getusesByCondition6(List<Integer> roleids) {
        SqlSession sqlSession= MyBatisUtil.getSession();
        List<Uses> list=null;
        try{
            list=sqlSession.getMapper(UsesDao.class).getusesByCondition6(roleids);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    @Override
    public int addUses(Uses uses) {
        int count=0;
        SqlSession sqlSession= MyBatisUtil.getSession();

        try{
            count=sqlSession.getMapper(UsesDao.class).addUses(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return count;
    }

    @Override
    public int delUses(Integer userid) {
        int count=0;
        SqlSession sqlSession= MyBatisUtil.getSession();

        try{
            count=sqlSession.getMapper(UsesDao.class).delUses(userid);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return count;
    }

    @Override
    public int updateUses(Uses uses) {
        int count=0;
        SqlSession sqlSession= MyBatisUtil.getSession();

        try{
            count=sqlSession.getMapper(UsesDao.class).updateUses(uses);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return count;
    }
}
